cmake_minimum_required(VERSION 3.13.4)

find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)
BISON_TARGET(MyParser grammar.y ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp)
FLEX_TARGET(MyScanner token.l  ${CMAKE_CURRENT_BINARY_DIR}/token.cpp)
ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_executable(fajal main.cpp types.cpp
    ${BISON_MyParser_OUTPUTS}
    ${FLEX_MyScanner_OUTPUTS}
)

target_link_libraries(fajal PUBLIC ${FLEX_LIBRARIES})

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Set your project compile flags.
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${LLVM_INCLUDE_DIRS}/llvm-13)
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})

# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader native)
# llvm_map_components_to_libnames(llvm_libs support core irreader orcjit native executionengine)

# Link against LLVM libraries
target_link_libraries(fajal INTERFACE ${llvm_libs})

add_library(types STATIC types.cpp)
add_library(graph STATIC graphviz.cpp)

add_subdirectory(./expr)
add_subdirectory(./YY)

if(BAN_BACKEND)
    # 暂时不使用后端
    add_definitions(-DBAN_BACKEND)
    target_link_libraries(fajal PUBLIC expr types graph yytype c rt dl pthread z m)
else(BAN_BACKEND)
    add_subdirectory(./x86)
    target_link_libraries(fajal PUBLIC expr types graph yytype x86emit c rt dl pthread z m)
endif(BAN_BACKEND)

# add_custom_command(
#     TARGET fajal POST_BUILD
#     COMMAND ${CMAKE_COMMAND} -E copy
#     ${CMAKE_SOURCE_DIR}/test/test.kt
#     ${CMAKE_BINARY_DIR}/src/test.kt
# )
